package com.aspect;

import com.bean.anno.LogAnno;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.UUID;

@Aspect
@Component
@Slf4j
public class LogAspect {

    //表示只要是加了@LoginAnno这个注解的方法，都会被拦截
    @Pointcut("@annotation(com.bean.anno.LogAnno)")
    public void pointcut() {

    }

    @Around("pointcut()")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {


        MethodSignature signature = (MethodSignature) joinPoint.getSignature();//
        Method method = signature.getMethod();

        LogAnno logAnno = method.getAnnotation(LogAnno.class);
        String apiName = logAnno.apiName();
        boolean flag = logAnno.flag();
        if(flag){
            log.info(apiName+"调用");
            Object[] args = joinPoint.getArgs();//获取得是切点中的参数信息
            for(Object obj:args){
                log.info("参数"+obj.toString());
            }
        }


        Object result = null;
        try {
            result = joinPoint.proceed();//执行切点方法
            if(flag){
                log.info(apiName+"结果");
                log.info(result.toString());
            }
        } catch (Throwable e) {
            log.error(e.toString());
            throw e;
        }
        return result;
    }


}
